package com.xzx.shop_front_api.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Queue;

@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmq.queue.name}")
    private String queueName;

    @Value("${rabbitmq.exchange.name}")
    private String exchangeName;

    @Value("${rabbitmq.routing.key}")
    private String routingKey;

    // 创建队列
    @Bean
    public Queue queue() {
        // 队列名称，是否持久化，是否排他，是否自动删除，其他参数
        return new Queue(queueName, true, false, false);
    }

    // 创建交换机
    @Bean
    public TopicExchange exchange() {
        // 交换机名称，是否持久化，是否自动删除，其他参数
        return new TopicExchange(exchangeName, true, false);
    }

    // 绑定队列和交换机
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue())
                .to(exchange())
                .with(routingKey);
    }
}
